<?
	global $table_edit, $config_xml_list, $config_xml_edit, $db;
	
	$table_edit = "categories";
	
	$sql = "select * from config_view where active = 1 and type = 1 and alias = '".$table_edit."'";
	//echo $sql;
	$arr = $db->getRow($sql);
	$config_xml_list = isset($arr)?$arr['values']:'11111';
	
	$sql = "select * from config_view where active = 1 and type = 2 and alias = '".$table_edit."'";
	//echo $sql;
	$arr = $db->getRow($sql);
	$config_xml_edit = isset($arr)?$arr['values']:'11111';
	//echo $config_xml_edit;
?>
<?
	include("ckeditor_config.php");	
switch($act){
	
	case "list_full":
	ShowListFull();	
	$tpl="list_full";
	break;
	
	case "add_full":
	case "edit_full":	
	EditFull();
	$title_page = "Chỉnh Sửa";
	$tpl="edit_full";
	break;

	
	case "addsm_full":
	case "editsm_full":
	EditSMFull();
	break;
	
	case "del":
	Del();
	break;
		
	case "changeactive":
	ChangeActive("active");
	break;
	
	case "changehaschild":
	ChangeActive("has_child");
	break;
	
	case "dellist":
	DelList();
	break;
	
	case "show":
	ChangeShowList("active");
	break;
	
	case "hide":
	ChangeHideList("active");
	break;
		
	case "order":
	Order();
	break;
	
	default:
	//ShowListFull();
	ShowListCategories();
	$title_page = "CMS - Sản phẩm";
	$tpl="list";
		
//	case "edit":	
//	Edit();
//	$title_page = "Chỉnh Sửa";
//	$tpl="edit";
//	break;
//	
//	case "add":	
//	$title_page = "Thêm";
//	$tpl="edit";
//	break;

//	case "addsm":
//	case "editsm":
//	Editsm();
//	break;
	
}

function ShowListCategories()
{	
	global $db,$element,$page,$plpage,$set_per_page,$c;
	$set_per_page=20; 
	$pid = isset($_GET['cid'])? $_GET['cid']:'121';
	$sql="select * from categories where pid=".$pid." order by num asc, id asc ";
	$c = $db->numRows($db->query($sql));
	$plpage = plpage($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);	
	$element = $db->getAll($sqlstmt);
}

function EditSMFull()
{
	global $db,$act, $FullUrl, $table_edit;	
	
	$sql = "select column_name from information_schema.columns where table_name='".$table_edit."'";
	
	$arr_table = $db->getAll($sql);
	
	$arr = NULL;
	
	if(isset($_GET['cid']))
		$arr['pid'] = $_GET['cid'];
		
	for($i = 0; $i < count($arr_table);$i++)
	{
		$name_label = $arr_table[$i]['column_name'];
		//$type_label = $arr_table[$i]['data_type'];
		
		if($name_label == 'cid')
		{
			if(isset($_GET['cid']))
				$arr['pid'] = $_GET['cid'];
			else
				$arr = ArrPostClean2($arr, 'pid', 'cat');
		}
		else if($name_label == 'active')
		{
			$arr = ArrPostCleanActive($arr, $name_label);
		}
		else if($name_label == 'showed')
		{
			$arr = ArrPostCleanActive($arr, $name_label);
		}
		else if($name_label == 'has_child')
		{
			$arr = ArrPostCleanActive($arr, $name_label);
		}
		else if($name_label == 'img')
		{
			if(isset($_FILES['img']['name'] ) && $_FILES['img']['size']>0){
				$img = $_FILES['img']['name'];
				$start = strpos($img,".");
				$type = substr($img,$start,strlen($img));
				CheckUpload($type);
				
				$filename = strtolower(substr($img, 0,$start));
				$filename = RenameFile($filename).$type;
				if(file_exists( "./upload/images/" . $filename)){
					$filename = substr($img, 0, $start);
					$filename = $_POST["unique_key_vn"] . '-' . time() . $type;
					
				}
				else
				{
					// Khong co
				}
				copy($_FILES['img']['tmp_name'], "./upload/images/" . $filename) ;
				$arr['img'] = "upload/images/" . $filename;
			}
		}
		else
		{
			$arr = ArrPostClean($arr, $name_label);
		}

	}
	// Phần mở rộng của table
	
	$value_answer = GetAnswer();	
	if($value_answer != '')
		$arr['answer'] = $value_answer;
		
	// Kết thúc phần mở rộng
	
	if ($act=="addsm_full")
	{
		
		// Xử lý Image
		$postId = vaInsert($table_edit,$arr);
		$msg="Add successfully";				
	}
	else
	{
		// Xử lý Image
		$id=$_POST['id'];
		if (isset($arr['img'])){
			$sqlstmt="select img from ".$table_edit." where id=$id";
			$r = $db->getRow($sqlstmt);
			if(file_exists($r['img'])) unlink($r['img']);
		}
		vaUpdate($table_edit, $arr,' id='.$id);	
		$msg="Đã Edit successfully";	
	}
	
	$msg="Add successfully";
	$_SESSION['mess'] = $msg;
	$page="admin-pro.html?do=".$table_edit. (isset($_GET['page'])?'&page='.$_GET['page']:'')."&act=list&cid=121";
	page_transfer2($page);		
}


function GetAnswer()
{
	$check_a = isset($_POST['answer_a_check'])?'a':'';
	$check_b = isset($_POST['answer_b_check'])?'b':'';
	$check_c = isset($_POST['answer_c_check'])?'c':'';
	$check_d = isset($_POST['answer_d_check'])?'d':'';
	$check_e = isset($_POST['answer_e_check'])?'e':'';
	$check_f = isset($_POST['answer_f_check'])?'f':'';
	
	$value = '';
	if($check_a)
		$value = $value.$check_a.';';
	if($check_b)
		$value = $value.$check_b.';';
	if($check_c)
		$value = $value.$check_c.';';
	if($check_d)
		$value = $value.$check_d.';';
	if($check_e)
		$value = $value.$check_e.';';
	if($check_f)
		$value = $value.$check_f;
	
	return $value;						
	
}
?>
<?
//function Editsm()
//{
//	global $db,$act, $FullUrl, $table_edit;	
//	
//	$arr = NULL;
//	
//	$arr = ArrPostClean($arr, 'name_vn');
//	$arr = ArrPostClean($arr, 'question');
//	$arr = ArrPostClean($arr, 'answer_a');
//	$arr = ArrPostClean($arr, 'answer_b');
//	$arr = ArrPostClean($arr, 'answer_c');
//	$arr = ArrPostClean($arr, 'answer_d');
//	$arr = ArrPostClean($arr, 'answer_e');
//	$arr = ArrPostClean($arr, 'answer_f');
//	$arr = ArrPostClean2($arr, 'cid', 'cat');
//	$value_answer = GetAnswer();	
//	if($value_answer != '')
//		$arr['answer'] = $value_answer;
//	
//	
//	if ($act=="addsm")
//	{
//		// Xử lý Image
//		$postId = vaInsert($table_edit,$arr);
//		$msg="Add successfully";				
//	}
//	else
//	{
//		// Xử lý Image
//		$id=$_POST['id'];
//		vaUpdate($table_edit, $arr,' id='.$id);	
//		$msg="Đã Edit successfully";	
//		
//		/*if (isset($arr['img'])){
//			$sqlstmt="select img from `question_choice` where id=$id";
//			$r = $db->getRow($sqlstmt);
//			if(file_exists($r['img'])) unlink($r['img']);*/
//	}
//	//print_r($arr);
//	
//	$msg="Add successfully";
//	$_SESSION['mess'] = $msg;
//	$page="admin-pro.html?do=".$table_edit. (isset($_GET['page'])?'&page='.$_GET['page']:'');
//	page_transfer2($page);		
//}

//function ShowList()
//{
//	global $questions, $table_edit;
//	
//	$questions = ShowListArr($table_edit);
//}
//function Edit()
//{
//	global $question_choice, $table_edit;
//	
//	$question_choice = EditElement($table_edit);
//}
?>